<template>
  <view class="content">
    <image class="logo" src="@/static/dust2.png"></image>
    <view class="text-area">
      <text class="title">Hello Aix-App</text>
    </view>
  </view>
</template>

<script>
  import { sendSmsCode, bindMobileDeviceId } from '@/api/system/user'
  export default {
    onLoad: function() {
		// 获取用户是否已绑定此设备？
		const bindStatus = uni.getStorageSync('bindStatus')
		console.log('进来了吗', bindStatus)
		if(bindStatus === 0 || bindStatus === 3){
			let contents = {
				'content_0': '检测到您首次登录此设备，是否设置该设备为授信设备？',
				'content_3': '检测到您更换了登录设备，是否重新绑定此设备为授信设备？'
			}
			uni.showModal({
				title: '提示',
				cancelText: '临时使用',
				content: contents['content_' + bindStatus],
				success: function (res) {
					if (res.confirm) {
						console.log('用户点击确定');
						uni.showLoading({
							title: '发送中'
						})
						sendSmsCode({'scene':'bind_device'}).then(resp=>{
							console.log('resp=', resp)
							uni.showModal({
								title: '验证码已发送',
								content: resp.msg,
								showCancel: false,
								confirmText: '好的',
								success: function (res) {
									if (res.confirm) {
										uni.showModal({
											title: '短信验证码',
											placeholderText: '',
											content: '',
											editable: true,
											success: function (res) {
												if (res.confirm) {
													const inputCode = res.content
													const deviceId = uni.getSystemInfoSync().deviceId;
													const data = {
														smsCode: inputCode,
														deviceId: deviceId
													}
													bindMobileDeviceId(data).then(resp=>{
														if(resp.code === 200) {
															uni.showToast({
																title: '绑定成功'
															})
															uni.setStorageSync('deviceUUId', resp.deviceUUId)
															uni.setStorageSync('bindStatus', 1)
														}
													})
													
												} else if (res.cancel) {
													console.log('用户点击取消');
												}
											}
										});
									} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							});
						}).finally(()=>{
							uni.hideLoading()
						});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		}
    }
  }
</script>

<style>
  .content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }

  .logo {
    height: 200rpx;
    width: 200rpx;
    margin-top: 200rpx;
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 50rpx;
  }

  .text-area {
    display: flex;
    justify-content: center;
  }

  .title {
    font-size: 36rpx;
    color: #8f8f94;
  }
</style>
